package com.rh.controller;

import com.rh.entity.vo.EduTeacherPageVo;
import com.rh.entity.EduTeacher;
import com.rh.result.ResultVo;
import com.rh.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Api(tags = "教师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;
    //1.获取讲师所以数据
    @GetMapping("findAll")
    @ApiOperation(value = "1.获取所有教师列表")
    public ResultVo findAllTeacher(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return ResultVo.ok().data("list",list);
    }
    //2.讲师逻辑删除
    @ApiOperation("2.逻辑删除教师")
    @DeleteMapping("{id}")
    public ResultVo deleteTeacher(@ApiParam(name = "id",value = "教师ID",required = true) @PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        if(flag){
            return ResultVo.ok();
        }else {
            return ResultVo.error();
        }
    }
    //3.分页查询教师
    @ApiOperation("3.分页查询教师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public ResultVo findTeacherPage(@ApiParam(name = "current",value = "当前页",required = true)@PathVariable long current,
                                   @ApiParam(name = "limit",value = "每页记录数",required = true) @PathVariable long limit){
        ResultVo teacherPage = eduTeacherService.findTeacherPage(current, limit);
        return teacherPage;
    }
    //4.条件查询带分页
    @ApiOperation("4.条件按查询带分页")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public ResultVo findTeacherCondition(@PathVariable Long current,
                                         @PathVariable Long limit,
                                         @RequestBody(required = false) EduTeacherPageVo eduTeacherVo){
        return eduTeacherService.pageQuery(current,limit, eduTeacherVo);
    }
    //5.添加教师
    @ApiOperation("5.添加教师")
    @PostMapping("addTeacher")
    public ResultVo saveTeacher(@RequestBody(required = false)EduTeacher eduTeacher){
        return eduTeacherService.addTeacher(eduTeacher);
    }
    //6.根据id查询教师
    @ApiOperation("6.根据id查询教师")
    @GetMapping("{id}")
    public ResultVo getTeacherById(@PathVariable String id) {
        EduTeacher teacher = eduTeacherService.getById(id);
        return ResultVo.ok().data("teacher", teacher);
    }
    //7.根据id修改教师数据
    @ApiOperation("7.根据id修改教师数据")
    @PutMapping("updateTeacher")
     public ResultVo updateTeacher(@RequestBody  EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);

        if(flag){
            return ResultVo.ok();
        }else {
            return ResultVo.error();
        }
    }
    //测试非restful风格.分页查询教师
    @ApiOperation("8.分页查询教师")
    @GetMapping("pageTest")
    public ResultVo testPage(Long current,Long limit){
        ResultVo teacherPage = eduTeacherService.findTeacherPage(current, limit);
        return teacherPage;
    }
}

